package com.tigercar.servicemanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tigercar.common.pojo.PageResult;
import com.tigercar.common.utils.BeanHelper;
import com.tigercar.servicemanagement.mapper.RoleMapper;
import com.tigercar.servicemanagement.pojo.Role;
import com.tigercar.servicemanagement.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;

    public PageResult<RoleVo> searchRoleList(Integer curPage, Integer pageSize, String roleName) {
        IPage<Role> iPage = new Page<>(curPage,pageSize);
        QueryWrapper<Role> queryWrapper = Wrappers.query();
        if(StringUtils.isNotEmpty(roleName)){
            queryWrapper.like("role_name",roleName);
        }
        iPage = roleMapper.selectPage(iPage,queryWrapper);
        List<Role> roleList = iPage.getRecords();
        List<RoleVo> roleVos = BeanHelper.copyWithCollection(roleList, RoleVo.class);
        PageResult<RoleVo> pageResult = new PageResult<>(iPage.getTotal(), iPage.getPages(), roleVos);
        return pageResult;
    }

    public void deleteRole(Integer roleId) {
        roleMapper.deleteById(roleId);
    }
}
